package impl

import (
	"ce-cloud-service/internal/services/base/user/db/model"
	"ce-cloud/pkg/data_source/d_db"
	"ce-cloud/pkg/log"
	"gorm.io/gorm"
)

type UserImpl struct {
	d_db.SQLBaseImpl
}

func NewUserImpl(db ...*gorm.DB) *UserImpl {
	repo := new(UserImpl)
	repo.InitEngine(db...)
	return repo
}

func (t *UserImpl) Create(args ...model.User) (err error) {
	return t.GetEngine().Model(model.User{}).Create(args).Error
}

func (t *UserImpl) Get(arg model.User) (data model.User, err error) {
	engine := t.GetReadEngine().Model(model.User{})
	if arg.Uid != 0 {
		engine.Where("uid=?", arg.Uid)
	}
	if arg.Account != "" {
		engine.Where("account=?", arg.Account)
	}
	if arg.NickName != "" {
		engine.Where("nick_name=?", arg.NickName)
	}
	if arg.Password != "" {
		engine.Where("password=?", arg.Password)
	}
	if err = engine.First(&data).Error; err != nil {
		log.Error("get user error: ", err)
	}
	return
}
